<template>
	<view style="background-color: #f0f2f5;">
		<u-swiper :height="160" :list="imgList" :title="false" @click="imgListClick" style="padding: 10px;"></u-swiper>
		<view style="background-color: #ffffff;margin: 20rpx;" v-if="isShow('sys:sell:listSell')">
			<view class="workbench-title">销售订单</view>
			<view class="toolbar">
				<u-grid class="grid" :col="4" :border="false">
					<u-grid-item :index="1" @click="navTo('/pages/supplyChain/sellHome/index')">
						<!-- 					<view class="home-icon icon-color04">
													<i class="iconfont icon-xiaoshoudingdanliebiao" style="color: #00aa7f;"></i>
												</view> -->
						<view>
							<image src="@/static/icon/xiaoshoudingdan.png" class="iconStyle"></image>
						</view>
						<view class="grid-text">订单列表</view>
					</u-grid-item>
					<u-grid-item :index="0" @click="navTo('/pages/supplyChain/sellOrder/index')">
						<!-- 					<view class="home-icon" style="color:#ef8e35 ">
									<i class="iconfont icon-xinzengxiaoshoudingdan"></i>
								</view> -->
						<view>
							<image src="@/static/icon/xiadan1.png" class="iconStyle"></image>
						</view>
						<view class="grid-text">销售下单</view>
					</u-grid-item>
					<!-- 				<u-grid-item :index="1" @click="navTo('/pages/sys/workbench/add-form')">
								<view class="home-icon icon-color04">
									<i class="iconfont icon-caigoudingdanliebiao" style="color: #5555ff ;"></i>
								</view>
								<view class="grid-text">采购订单</view>
							</u-grid-item> -->

				</u-grid>
			</view>
		</view>
		<view style="background-color: #ffffff;margin: 20rpx;" v-if="isShow('sys:sell:listSale')">
			<view class="workbench-title">采购订单</view>
			<view class="toolbar">
				<u-grid class="grid" :col="4" :border="false">
					<u-grid-item :index="0" @click="navTo('/pages/supplyChain/saleOrder/index')">
						<!-- 					<view class="home-icon icon-color04">
							<i class="iconfont icon-kehuguanli" style="color: red;"></i>
						</view> -->
						<view>
							<image src="@/static/icon/caigoudan.png" class="iconStyle"></image>
						</view>
						<view class="grid-text">订单列表</view>
					</u-grid-item>

				</u-grid>
			</view>
		</view>
		<view style="background-color: #ffffff;margin: 20rpx;" v-if="isShow('sys:package:logistic')">
			<view class="workbench-title">物流管理</view>
			<view class="toolbar">
				<u-grid class="grid" :col="4" :border="false">
					<u-grid-item :index="0" @click="navTo('/pages/supplyChain/wuliu/home')">
						<!-- 					<view class="home-icon icon-color04">
							<i class="iconfont icon-kehuguanli" style="color: red;"></i>
						</view> -->
						<view>
							<image src="@/static/icon/wuliuguanli.png" class="iconStyle"></image>
						</view>
						<view class="grid-text">物流分配</view>
					</u-grid-item>

				</u-grid>
			</view>
		</view>
		<view style="background-color: #ffffff;margin: 20rpx;" v-if="isShow('sys:customer')">
			<view class="workbench-title">客户管理</view>
			<view class="toolbar">
				<u-grid class="grid" :col="4" :border="false">
					<u-grid-item :index="0" @click="navTo('/pages/supplyChain/customer/index')">
						<!-- 					<view class="home-icon icon-color04">
							<i class="iconfont icon-kehuguanli" style="color: red;"></i>
						</view> -->
						<view>
							<image src="@/static/icon/kehuguanli.png" class="iconStyle"></image>
						</view>
						<view class="grid-text">客户管理</view>
					</u-grid-item>

				</u-grid>
			</view>
		</view>
<!-- 		<view style="background-color: #ffffff;margin: 20rpx;" v-if="isShow('sys:target:saleSet')||isShow('sys:customer')">
			<view class="workbench-title">个人计划</view>
			<view class="toolbar">
				<u-grid class="grid" :col="4" :border="false">
					<u-grid-item :index="0" @click="navTo('/pages/supplyChain/plan/sellPlan')">
						<view>
							<image src="@/static/icon/sellPlan.png" class="iconStyle"></image>
						</view>
						<view class="grid-text">销售计划</view>
					</u-grid-item>
					<u-grid-item :index="1" @click="navTo('/pages/supplyChain/plan/customerPlan')">
						<view>
							<image src="@/static/icon/customerPlan.png" class="iconStyle"></image>
						</view>
						<view class="grid-text2">客户开发计划</view>
					</u-grid-item>
				</u-grid>
			</view>

		</view> -->
		<!-- <u-divider text="我是有底线的哟!" :dashed="true"></u-divider> -->
		<up-tabbar :value="currentTabbar" @change="name => currentTabbar = name" :fixed="true" :placeholder="true"
			:border="true" :safeAreaInsetBottom="true">
			<up-tabbar-item text="云工作台" icon="arrow-leftward" @click="goback" />
			<up-tabbar-item text="供应链工作台" icon="home" />
			<up-tabbar-item :text="this.user.name" icon="account" @click="goUser"></up-tabbar-item>
		</up-tabbar>

		<u-modal :show="modalShow" title="提示" @confirm="confirmModal" confirmText="返回云平台">
			<view class="slot-content">
				<u-text type="error" :text="modalContent"></u-text>
				<!-- 	<rich-text :nodes="modalContent"></rich-text> -->
			</view>
		</u-modal>

		<u-toast ref="uToast"></u-toast>
		<upVersion :upLoadUrl="upLoadUrl" :newVision="newVision" :appStoreUrl='appStoreUrl' :show="show" :log="log" @close="close"></upVersion>
	</view>
</template>

<script>
	import baseApi from '@/apis/gyl/base.js';
	import upVersion from "@/components/as-upVersion/as-upVersion.vue"
	import {
		useLoginStore,
		useUserStore
	} from '@/pinia-store/user';
	import {
		mapStores
	} from 'pinia';
	import checkappupdate from '/js_sdk/wonyes-checkappupdate/wonyes/checkappupdate.js'
    import hasPermission from '@/utils/permission';
	export default {
		components: {
		    upVersion
		},
		data() {
			return {
				imgList: ['/static/supplyChain/banner.jpg', '/static/supplyChain/1500021428.jpg',
					'/static/supplyChain/1500022158.jpg', '/static/supplyChain/new-img-d.png'
				],
				currentTabbar: 1,
				user: {
					name: "获取中"
				},
				modalShow: false,
				modalContent: '',
				isShowKey:false,//用于重新渲染页面
				upLoadUrl: "", // 安卓下载链接
				newVision: "", // 最新版本号
				appStoreUrl: '' ,// AppStore链接,
				log:'',
				show:false
			}
		},
		methods: {
			isShow(code){
				// console.info(this.isShowKey)
				return this.isShowKey&&hasPermission(code);
			},
			message(){
				this.$refs.uToast.show({
					type: "default",
					title: "默认主题",
					message: "正在研发中哦",
					complete() {
						params.url &&
							uni.navigateTo({
								url: params.url,
							});
					},
				});
			},
			imgListClick(index) {
				console.log(`点击了第${index + 1}页图片`)
			},
			goback() {
				// uni.redirectTo({
				// 	url: '/pages/workspace/home/index'
				// });
        uni.navigateBack({ delta: 1 });
      },
			goUser() {
				uni.redirectTo({
					url: '/pages/supplyChain/home/user?userName=' + this.user.name + '&departmentName=' + this
						.user.departmentName
				});
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				});
			},
			confirmModal() {
				this.modalShow = false;
				this.goback();
			},
			async loginNoPass(data) {
				const res = await baseApi.loginNoPass(data);
				if (res.code == 200) {
					console.info(res.token)
					uni.setStorageSync('gylToken', res.token);
					this.getUserInfo();
				} else if (res.code == 100) {
					uni.hideLoading()
					this.modalContent = res.message;
					this.modalShow = true;
				} else if (res.code == 300) {
					uni.hideLoading()
					this.modalContent = res.message;
					this.modalShow = true;
				}
			},
			async getUserInfo() {
				const res = await baseApi.getUserInfo();
				if (res.success) {
					this.user = res.data;
					if(this.user.roles && this.user.roles.length>0){
						uni.setStorageSync("codeList",this.user.roles);
						this.isShowKey = true;
					}
				}
				uni.hideLoading()
			},

			/**
			 * 获取版本
			 */
			checkVersion() {
				var info = uni.getSystemInfoSync();
				uni.request({
					url: 'http://121.229.107.81:81/api/version/getVersion', //仅为示例，并非真实接口地址。
					data: {
						platform: plus.os.name.toLocaleLowerCase(),
						version: info.appWgtVersion
					},
					header: {
						'content-type': 'application/json'
					},
					method: 'POST',
					dataType: 'json',
					success: (res) => {
						console.info(res)
						//此处调用后端接口返回当前版本号和下载地址
						//回调赋值
						if(res.data.data.code != 0){
							return;
						}
						this.newVision = res.data.data.version
						this.appStoreUrl = ''
						this.upLoadUrl = res.data.data.url
						this.log = res.data.data.log
						this.show = true
					}
				});
			},
			close(){
				this.show = false;
			},
		},
		onLoad(option) {
			console.info(option)
			if (!option.isShow) {
				// checkappupdate.check({
				// 	isShow: 1,
				// })
				// 更改至全局
				// #ifdef APP
				// this.checkVersion();
			  // #endif
			}

		},
		mounted() {
			const phone = this.userStore.user.phone;
			console.info(phone)
			uni.showLoading({
				title: '加载获取权限中',
				mask:true
			});
			this.loginNoPass({
				phone: phone
			});
		},
		computed: {
			...mapStores(useLoginStore, useUserStore)
		},
	}
</script>

<style>
	.u-swiper {
		background-color: white !important;
	}

	.workbench-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		padding: 5px 30rpx;
		margin-bottom: 10px;
	}

	.home-icon {
		width: 48px;
		height: 48px;
		align-items: center;
		border-radius: 6px;
		box-sizing: border-box;
		display: flex;
		justify-content: center;
		color: #ff5500;
		font-size: 26px;
	}

	.grid-text {
		font-size: 24rpx;
		padding: 15rpx;
		color: #202328;
	}

	.grid-text2 {
		font-size: 23rpx;
		padding: 15rpx;
		color: #202328;
	}

	.toolbar {
		margin-bottom: 10px;
	}

	.iconStyle {
		width: 95rpx;
		height: 95rpx;
	}

	::v-deep .u-border-top {
		border-top: 1px solid #d9d9d9 !important;
	}
</style>
